package com.itheima.service.impl;

import com.itheima.exception.BusinessException;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.LoginInfo;
import com.itheima.service.LoginService;
import com.itheima.util.JwtUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private EmpMapper empMapper;

    /**
     * 登录
     */
    @Override
    public LoginInfo login(Emp emp) {
        Emp e = empMapper.login(emp);
        if (e == null) {
            throw new BusinessException("账号或密码错误");
        }

        //生成JWT令牌
        //  先准备用户身份信息，这些信息要放到令牌的payload中。注意：不要将敏感信息放以令牌中
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",e.getId());
        claims.put("username",e.getUsername());
        //  生成令牌JWT字符串
        String token = JwtUtils.generateJwt(claims);
        return new LoginInfo(e.getId(),e.getUsername(),e.getName(),token);
    }
}
